package pl.un4giv3n.mightvsmagic.action;

import java.util.Date;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.Message;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.utils.FormatterUtil;

/**
 * @author andrzejewskia
 * a go-to-jsp page action
 */
public class ComposeMessagePageAction extends PersistenceAction{

	private static final long serialVersionUID = 1L;
	private User user;
	private long messageId;
	private String replyBody = "";
	private String replyTo = "";
	private String replySubject = "";
	
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if(user == null){
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		if(messageId != 0){
			Message msg = entityManager.find(Message.class, messageId);
			StringBuilder sb = new StringBuilder();
			sb.append("Dnia ").append(FormatterUtil.formatDate(new Date(msg.getSendTime())))
				.append(" ").append(msg.getSenderName()).append(" napisał(a):\n");
			sb.append(msg.getMessage());
			replyBody = sb.toString();
			replySubject = "Re:"+msg.getSubject();
			
		}
		entityManager.close();
		return SUCCESS;
	}
	
	public User getUser() {
		return user;
	}

	public long getMessageId() {
		return messageId;
	}

	public void setMessageId(long messageId) {
		this.messageId = messageId;
	}

	public String getReplyTo() {
		return replyTo;
	}

	public void setReplyTo(String replyTo) {
		this.replyTo = replyTo;
	}
	
	public String getReciverLogin(){
		return replyTo;
	}
	
	public String getSubject(){
		return replySubject;
	}
	
	public String getMessage(){
		return replyBody;
	}
}